﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChurchData.Business
{
    public class LKKeywordBO : ChurchDataBusinessBase, ILKTable
    {
        public List<GenericLKTableResults> GetActiveRecords()
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());
            var x =
                    from oLK in oDB.LKKeywords
                    where oLK.ActiveInd == true
                    orderby oLK.DescriptionText ascending
                    select oLK;


            List<GenericLKTableResults> oList = new List<GenericLKTableResults>();
            foreach (LKKeyword oLK in x)
            {
                GenericLKTableResults oGLK = new GenericLKTableResults();
                oGLK.Pk = oLK.LKKeywordPK;
                oGLK.Description = oLK.DescriptionText;
                oList.Add(oGLK);
            }
            return oList;

        }

        public int InsertRecord(string description)
        {
            using (ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString()))
            {
                LKKeyword oLK = new LKKeyword();
                oLK.ActiveInd = true;
                oLK.DescriptionText = description;
                oLK.LastUpdUser = UserInformation.UserName;
                oDB.LKKeywords.InsertOnSubmit(oLK);
                oDB.SubmitChanges();
                return oLK.LKKeywordPK;
            }
        }

        public bool ModifyRecord(int pk, string description)
        {
            using (ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString()))
            {
                var x = (from oLK in oDB.LKKeywords
                         where oLK.LKKeywordPK == pk
                         select oLK).Single();
                x.DescriptionText = description;
                x.LastUpdUser = UserInformation.UserName;
                oDB.SubmitChanges();
                return true;
            }
        }

        public string GetDescriptionByPK(int pk)
        {
            using (ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString()))
            {
                var x =
                        from oLK in oDB.LKKeywords
                        where oLK.LKKeywordPK == pk
                        select oLK.DescriptionText;
                List<string> oList = x.ToList();
                return (oList.Count == 0) ? "" : oList[0];
            }
        }

        public bool SetToInactive(int pk)
        {
            using (ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString()))
            {
                var x = (from oLK in oDB.LKKeywords
                         where oLK.LKKeywordPK == pk
                         select oLK).Single();
                x.ActiveInd = false;
                x.LastUpdUser = UserInformation.UserName;
                oDB.SubmitChanges();
            }

            return true;
        }
    }//class
}
